package com.monkeygroup.mygoplus.common.imageview;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.monkeygroup.mygoplus.R;

import java.util.List;

public class DirsPopWindow extends PopupWindow {
    private int width; // PopUpWindow的宽度
    private int height; // PopUpWindow的高度
    private View convertView; // 代表PopUpWindow
    private ListView dirList; // 显示目录信息的ListView
    private List<FolderModel> datas; // ListView中显示的信息的列表

    public OnDirSelectListener listener;

    public interface OnDirSelectListener {
        void onDirSelected(FolderModel folder);
    }

    public void setOnDirSelectListener(OnDirSelectListener listener) {
        this.listener = listener;
    }

    public DirsPopWindow(Context context, List<FolderModel> datas) {
        calculateWidthAndHeight(context);
        convertView = LayoutInflater.from(context).inflate(R.layout.sideworks_popwindow, null);
        this.datas = datas;
        setContentView(convertView);
        setWidth(this.width);
        setHeight(this.height);
        setFocusable(true);
        setTouchable(true);
        setOutsideTouchable(true); // 外面可以点击
        setBackgroundDrawable(new BitmapDrawable()); // 点击外面的区域会使PopUpWindow消失
        // 点击外部的事件（让PopUpWindow消失）
        setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                    dismiss();
                    return true;
                }
                return false;
            }
        });

        initView(context);
        initEvent();
    }

    private void initView(Context context) {
        dirList = (ListView) convertView.findViewById(R.id.find_popwindow_lv_dirs);
        dirList.setAdapter(new DirAdapter(context, datas));
    }

    private void initEvent() {
        dirList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                ImageView image = (ImageView) view.findViewById(R.id.imageView);
                if(image.getVisibility()==View.GONE){
                    image.setVisibility(View.VISIBLE);
                }
                else{
                    image.setVisibility(View.GONE);
                }
                if (listener != null) {
                    listener.onDirSelected(datas.get(position));
                }
            }
        });
    }

    /**
     * 计算PopUpWindow的宽度和高度
     */
    private void calculateWidthAndHeight(Context context) {
        // 获取屏幕的宽和高
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(metrics);
        // 为PopUpWindow设置宽和高
        this.width = metrics.widthPixels;
        this.height = (int) (metrics.heightPixels * 0.7);
    }

    /**
     * PopUpWindow中ListView的适配器
     */
    private class DirAdapter extends ArrayAdapter<FolderModel> {
        private LayoutInflater inflater;
        private List<FolderModel> datas;

        public DirAdapter(Context context, List<FolderModel> objects) {
            super(context, 0, objects);
            inflater = LayoutInflater.from(context);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = inflater.inflate(R.layout.sideworks_popitem, parent, false);
                viewHolder.image = (ImageView) convertView.findViewById(R.id.find_popitem_iv_image);
                viewHolder.dirName = (TextView) convertView.findViewById(R.id.find_popitem_tv_dir);
                viewHolder.imgCount = (TextView) convertView.findViewById(R.id.find_popitem_tv_imgcount);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            FolderModel folder = getItem(position);
            // 重置
            viewHolder.image.setImageResource(R.mipmap.img_picture_selected);
            // 回调加载
            ImageLoader.getInstance(3, ImageLoader.Type.LIFO).loadImage(folder.getFirstImgPath(), viewHolder.image);
            viewHolder.dirName.setText(folder.getName());
            viewHolder.imgCount.setText(folder.getCount() + "");

            return convertView;
        }

        private class ViewHolder {
            ImageView image;
            TextView dirName;
            TextView imgCount;
        }
    }
}


